package com.score.student.domain.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author hyh
 * @version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "分页查询实体")
public class PageQuery {

    @ApiModelProperty("页码")
    private Integer pageNo = 1;
    @ApiModelProperty("总页数")
    private Integer pageSize = 5;
    @ApiModelProperty("排序方式")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc = true;

    public <T> Page<T> toMpPage(OrderItem ... items){
        //创建查询条件
        Page<T> p = Page.of(pageNo, pageSize);
        //创建排序条件
        if (StrUtil.isNotBlank(sortBy)){
            //不为空
            p.addOrder(new OrderItem(sortBy,isAsc));
        }else if(items != null){
            p.addOrder(items);
        }
        return p;
    }

    public <T> Page<T> toMpPageDefault(String defaultSortBy , Boolean defaultIsAsc){
        return toMpPage(new OrderItem(sortBy, isAsc));
    }

    public <T> Page<T> toMpPageDefaultCreateTime(){
      return toMpPage(new OrderItem("create_time", false));
    }

    public <T> Page<T> toMpPageDefaultUpdateTime(){
        return toMpPage(new OrderItem("update_time", false));
    }

}
